import os.path
from PyPDF2 import PdfReader, PdfWriter


# a4到a3分割主函数
def manage_pdf(input_file, output_file):
    def a4_output():
        pdf_a4_writer = PdfWriter()
        a4_index_pages = pdf_reader.pages[head_page]
        pdf_a4_writer.add_page(a4_index_pages)
        a4_text_pages = pdf_reader.pages[tail_page]
        pdf_a4_writer.add_page(a4_text_pages)
        output_a4 = open("{}/(以A4双面打印) {}".format(output_file, file_name), "wb")
        pdf_a4_writer.write(output_a4)
        output_a4.close()

    # 判断是否分割a4小页的函数
    def is_skip_a4_page(num: int) -> bool:
        """判断整数是否属于起始值为6、公差为4的等差数列"""
        return (num >= 6) and ((num - 6) % 4 == 0)

    file_name = os.path.basename(input_file)
    pdf = open(input_file, "rb")
    pdf_reader = PdfReader(pdf)
    pdf_a3_writer = PdfWriter()
    pages = len(pdf_reader.pages)
    # head 从头开始计数； tail 从最后一页开始计数；page_num 总计数
    head_page = 0
    tail_page = int(pages - 1)
    page_num = 1

    # 判断首页
    if pages % 2 == 0:
        index_pages = pdf_reader.pages[tail_page]
        pdf_a3_writer.add_page(index_pages)
        text_pages = pdf_reader.pages[head_page]
        pdf_a3_writer.add_page(text_pages)
        head_page += 1
        tail_page -= 1
        page_num += 1
    else:
        if 0 < pages < 3:
            print()
        else:
            pdf_a3_writer.add_blank_page(width=210, height=29.7)
        text_pages = pdf_reader.pages[head_page]
        pdf_a3_writer.add_page(text_pages)
        head_page += 1
        page_num += 1

    # 判断总页数是否为奇数，如果是就加1
    if pages % 2 != 0:
        if_pages = pages + 1
    else:
        if_pages = pages

    # 按照奇偶数页排列每页
    while tail_page - head_page != 1:

        if page_num % 2 == 1:
            index_pages = pdf_reader.pages[tail_page]
            pdf_a3_writer.add_page(index_pages)
            text_pages = pdf_reader.pages[head_page]
            pdf_a3_writer.add_page(text_pages)
        else:
            index_pages = pdf_reader.pages[head_page]
            pdf_a3_writer.add_page(index_pages)
            text_pages = pdf_reader.pages[tail_page]
            pdf_a3_writer.add_page(text_pages)
        head_page += 1
        tail_page -= 1
        page_num += 1

    # 如果总页数在1-4页之间就执行if，其余页数执行else
    # if 0 < pages <= 4:
    #     index_pages = pdf_reader.pages[head_page]
    #     pdf_a3_writer.add_page(index_pages)
    #     text_pages = pdf_reader.pages[tail_page]
    #     pdf_a3_writer.add_page(text_pages)

    # 判断最后一页  在遇到一张a3与一张a4的情况下执行if
    if tail_page - head_page == 1:
        if is_skip_a4_page(if_pages):
            a4_output()
        else:
            index_pages = pdf_reader.pages[head_page]
            pdf_a3_writer.add_page(index_pages)
            text_pages = pdf_reader.pages[tail_page]
            pdf_a3_writer.add_page(text_pages)

    output_a3 = open("{}/(以A3双面打印) {}".format(output_file, file_name), "wb")
    pdf_a3_writer.write(output_a3)
    output_a3.close()


if __name__ == '__main__':
    in_p = "E:/程序/a4a3/6666.pdf"
    out_p = "E:/程序/a4a3"
    manage_pdf(in_p, out_p)
